package com.xuhao.didi.oksocket.btnview;

import com.xuhao.didi.oksocket.btnview.utils.AttrUtils;
import com.xuhao.didi.oksocket.btnview.help.RBaseHelper;
import com.xuhao.didi.oksocket.btnview.utils.TypedAttrUtils;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.element.Element;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.multimodalinput.event.MmiPoint;
import ohos.multimodalinput.event.TouchEvent;
/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class RippleHelper extends RBaseHelper<Text> implements Component.TouchEventListener, Component.DrawTask {
    AnimatorValue animatorValue = new AnimatorValue();
    private int sy;
    private float sx;
    private float zj;
    private Paint paint;
    private Color mTextRippleColor;
    private boolean isRipple;

    /**
     * 构造
     *
     * @param context 上下文
     * @param view text
     * @param attrs 属性集
     */
    public RippleHelper(Context context, Text view, AttrSet attrs) {
        super(context, view, attrs);
        mView.setTouchEventListener(this::onTouchEvent);
        mView.addDrawTask(this);
        mView.setTouchEventListener(this);
        initAttributeSet(context, attrs);
        paint = new Paint();
        paint.setColor(mTextRippleColor);
        paint.setAlpha(0.2f);
        paint.setStyle(Paint.Style.FILL_STYLE);
        animatorValue.setDuration(500);
        animatorValue.setCurveType(Animator.CurveType.INVALID);
    }

    /**
     * 初始化控件属性
     *
     * @param context
     * @param attrs
     */
    private void initAttributeSet(Context context, AttrSet attrs) {
        if (context == null || attrs == null) {
            return;
        }
        mTextRippleColor = AttrUtils.getStringToColor(attrs, "text_ripple_color", Color.TRANSPARENT);
        isRipple = AttrUtils.getStringToBool(attrs, "text_ripple", false);
    }

    /**
     * 点击事件
     *
     * @param component component
     * @param event 事件
     * @return 操作事件
     */
    @Override
    public boolean onTouchEvent(Component component, TouchEvent event) {
        if (isRipple && mView.isEnabled()) {
            sx = (float) (component.getWidth() / 2d);
            sy = component.getHeight() / 2;
            animatorValue.start();
        }
        return false;
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        if (isRipple && mView.isEnabled()) {
            animatorValue.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
                @Override
                public void onUpdate(AnimatorValue animatorValue, float value) {
                    zj = value;
                    if (value >= 1.0) {
                        paint.setAlpha(0f);
                    } else {
                        paint.setAlpha(0.2f);
                    }
                    mView.invalidate();
                }
            });
            if (mView.getWidth() > mView.getHeight()) {
                canvas.drawCircle(sx, sy, mView.getWidth() * zj, paint);
            } else {
                canvas.drawCircle(sx, sy, mView.getHeight() * zj, paint);
            }
        }
    }
}
